/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
ShareReferenceWindow = Ext.extend(Ext.app.Module, {
    id: 'shareref-win',
    autostart: false,
    store: null,
    formPanel: null,
    refId: '',
    window: null,
    init: function() {
        this.launcher = {
            text: 'Share Reference',
            iconCls: 'refwin-icon',
            handler: this.createWindow,
            scope: this
        };
    },
    
    onSubmitClick: function() {
    	var userId = this.formPanel.get(0).getValue();
    	if (!userId) {
    		alert('Please select a user.');
    		return;
    	}
    	ReferenceManager.shareReference(this.refId, userId, function(success) {
    		alert('Share request has been sent.');
    		this.window.hide();
    		this.window.close();
    	});
    },
    
    createWindow: function(referenceId) {
    	this.refId = referenceId;
    	var myId = 'shareref-win_'+new Date().getTime();
        var desktop = this.app.getDesktop();
        var ref = this;
        this.formPanel = new Ext.form.FormPanel({
        	items:[
        	    {
        	    	name: 'user', 
        	    	xtype: 'combo', 
        	    	fieldLabel: 'Share with user',
        	    	editable: false,
        	    	triggerAction:'all',
        	    	valueField: 'id',
        	    	displayField: 'username',
        	    	store: new Ext.data.JsonStore({
        	        	autoDestroy: true,
        	        	autoLoad: false,
        	        	totalProperty: 'results',
        	        	root: 'rows',
        	        	url: './references?action=getUserList',
        	        	fields: ['id', 'username']
        	        })
        	    }
        	], 
        	buttons:[{
        		text: 'Share my reference',
        		scope:ref,
        		handler: this.onSubmitClick
        	}]
        });
        this.window = desktop.createWindow({
            id: myId,
            title: 'Share Reference',
            width: 400,
            height: 200,
            x: 340,
            y: 400,
            iconCls: 'refwin-icon',
            shim: false,
            animCollapse: false,
            border: false,
            constrainHeader: true,
            collapsible: true,
            layout: 'fit',
            items: [ this.formPanel ]
        });
        this.window.show();
     }
});